package com.hxkj.controller;

import com.hxkj.domain.po.*;
import com.hxkj.service.AuthRoleService;
import com.hxkj.service.AuthSiteService;
import com.hxkj.service.AuthUserService;
import com.platform.common.dispatcher.util.ResultStructUtils;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @ClassName UserAuthController  用户控制器
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/5/19 14:46
 * @Version 1.0
 */

@RestController
@RequestMapping(value = "/user")
public class AuthUserController {

    @Autowired
    private AuthUserService authUserService;

    @Autowired
    private AuthSiteService siteService;


    /**
    * @author Mr.zheng
    * @Description getlist 获取用户列表
    * @Date 15:09 2018/5/19
    **/
    @RequestMapping(value = "/getlist",method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultPageStruct<AllocationAuthUser>> getUserBySlave(@RequestBody ParameterDataStruct<AuthUser> parameterDataStruct) throws Exception {
        return () -> ResultStructUtils.buildResultPageStruct(authUserService.getAuthUserBySlave(parameterDataStruct.getData()== null ? new AuthUser() : parameterDataStruct.getData(),parameterDataStruct.getPageTo()));
    }

    /**
    * @author Mr.zheng
    * @Description get  获取用户信息
    * @Date 15:20 2018/5/19
    **/
    @RequestMapping(value = "/get", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultDataStruct<AllocationAuthUser>> getAuthUser(@RequestBody AuthUser userAuth) throws Exception {

        return () -> authUserService.getAuthUser(userAuth.getUserId());
    }

    /**
    * @author Mr.zheng
    * @Description save 新增用户
    * @Date 15:19 2018/5/19
    **/
    @RequestMapping(value = "/save",method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> saveUser(@RequestBody AuthUser authUser) throws Exception {
        return () -> authUserService.saveAuthUser(authUser);
    }

    /**
    * @author Mr.zheng
    * @Description modify 修改用户
    * @Date 11:15 2018/5/21
    **/
    @RequestMapping(value="/modify",method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> updateAuthUser(@RequestBody AuthUser authUser) throws Exception {
        return () -> authUserService.updateAuthUser(authUser);
    }

    /**
    * @author Mr.zheng
    * @Description remove 删除用户信息
    * @Date 13:56 2018/5/22
    **/
    @RequestMapping(value = "/remove",method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public  Callable<ResultMessageStruct> deleteAuthUser(@RequestBody AuthUser authUser ) throws Exception{
        return () -> authUserService.deleteAuthUser(authUser);
    }

    /**
     * 根据用户查找对应的角色
     * @param authRole
     * @return
     * @throws Exception
     */

    @RequestMapping(value = "/role/getlist", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public  Callable<ResultDataStruct<AllocationUser>> getRoleAssignUser(@RequestBody AuthRole authRole) throws Exception{
        int i= 1;
        return () ->authUserService.getRoleAssignUser(authRole);
    }

    /**
     * 添加用户到角色
     * @param authRole
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addusertorole", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public  Callable<ResultMessageStruct> addUsertToRole(@RequestBody AuthRole authRole) throws Exception{
        return () -> authUserService.addUsertToRole(authRole);
    }

    /**
     * @author Mr.zheng
     * @Description saveUserSite 添加用户名下站点删除修改
     * @Date 19:26 2018/5/21
     **/
    @RequestMapping(value = "/setsite",method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> saveUserSite(@RequestBody AuthUser authUser) throws  Exception{
        return () -> siteService.saveUserSite(authUser);
    }

    /**
     * @author Mr.zheng
     * @Description getUserSite 根据用户ID查询其管理的站点
     * @Date 17:46 2018/5/21
     **/
    @RequestMapping(value = "/getsite",method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultDataStruct<AllocationSite>> getUserSite(@RequestBody AuthUser authUser) throws Exception{
        return () -> siteService.getUserSiteList(authUser.getUserId());
    }


    /**
     * 获取所有拥有自媒体角色的用户id集合
     */
    @RequestMapping(value = "/getagentlist",method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultPageStruct<AuthUserAgent>> getAgentList(@RequestBody ParameterDataStruct<AuthUserAgent> parameterDataStruct) throws Exception{
        return () -> ResultStructUtils.buildResultPageStruct(authUserService.getAgentList
                (parameterDataStruct.getData() == null ? new AuthUserAgent() : parameterDataStruct.getData(),parameterDataStruct.getPageTo()), "param=value");
    }

    /**
     * @author Mr.zheng
     * @Description getAuthRoleByUser
     * @Date 8:59 2018/6/14
     **/
    @RequestMapping(value = "/getrolebyuser", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<AuthRole>> getAuthRoleByUser(@RequestBody AuthUser authUser) throws Exception{
        return () -> authUserService.getAuthRoleByUser(authUser);
    }

    /**
     * @author Mr.zheng
     * @Description getAuthRoleByUser
     * @Date 8:59 2018/6/14
     **/
    @RequestMapping(value = "/getsitebyid", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<Site>> getSiteById(@RequestBody Site site) throws Exception{
        return () -> authUserService.getSiteById(site);
    }


    /**
     * @author Mr.zheng
     * @Description modify 修改用户
     * @Date 11:15 2018/5/21
     **/
    @RequestMapping(value="/modifypassword",method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> updateAuthUserPassword(@RequestBody AuthUser authUser) throws Exception {
        return () -> authUserService.updateAuthUserPassword(authUser);
    }
}
